<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <!-- 角色管理 -->
          <el-tab-pane label="角色管理" name="first">
            <!-- 按钮 -->
            <el-button icon="el-icon-plus" type="primary" @click="clickShowAdd">
              新增角色
            </el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" border :data="list">
              <el-table-column
                type="index"
                :index="indexMethod"
                label="序号"
                width="100"
              />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="showEdit(row.id)"
                  >
                    编辑
                  </el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4, 5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form
              v-loading="loading"
              label-width="120px"
              style="margin-top: 50px"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹层 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form
          ref="roleForm"
          :model="form"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitBtn">确认</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  reqGetRoleList,
  reqDeleteRole,
  reqAddRole,
  reqGetRoleDetail,
  reqUpdateRole
} from '@/api/setting'
import { mapState } from 'vuex'
import { reqGetCompanyById } from '@/api/company'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first',
      list: [],
      page: 1, // 当前页
      pagesize: 1, // 每页条数
      total: 0, // 总条数
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: ['blur', 'change']
          }
        ]
      },
      companyForm: []
    }
  },
  computed: {
    ...mapState('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      this.loading = true
      const { data } = await reqGetRoleList(this.page, this.pagesize)
      // console.log(data)
      this.list = data.rows
      this.total = data.total
      this.loading = false
    },
    // 修改了每页条数后, 会造成页数的变化, 推荐从第一页重新开始展示
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 更新每页条数
      this.pagesize = val
      // 重置当前页
      this.page = 1
      // 调用方法, 重新请求
      this.getRoleList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      // 更新当前页
      this.page = val
      // 调用方法, 重新请求渲染
      this.getRoleList()
    },
    // 自定义序号
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 删除
    delRole(id) {
      this.$confirm('确认要删除该角色吗?', '温馨提示')
        .then(async() => {
          await reqDeleteRole(id) // 调用删除接口
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          this.getRoleList() // 重新加载数据
          this.$message.success('删除角色成功')
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
      // 重置表单数据
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 显示弹窗
    clickShowAdd() {
      this.showDialog = true
    },
    // 点击弹窗的确定按钮
    submitBtn() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          // 编辑操作
          await reqUpdateRole(this.form)
          // 编辑成功
          this.$message.success('编辑成功')
        } else {
          await reqAddRole(this.form)
          // 添加成功
          this.$message.success('添加成功')
        }
        // 关闭弹框
        this.closeDialog()
        // 重新渲染
        this.getRoleList()
      })
    },
    // 点击编辑按钮--回显
    async showEdit(id) {
      const { data } = await reqGetRoleDetail(id)
      this.form = data
      this.showDialog = true
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.loading = true
      const { data } = await reqGetCompanyById(this.userInfo.companyId)
      this.companyForm = data
      this.loading = false
    }
  }
}
</script>

<style>
</style>
